from openpolitics.models import Person, InstitutionType, PositionType

def party_affiliation(person=None, pid=None):
    if person is None:
        person = Person.objects.get(id=pid)
    party_type = InstitutionType.objects.get(name="Party")
    position_type = PositionType.objects.get(name="Member", institution=party_type)
    parties = person.positions.filter(type=position_type)
    return parties[0].institution